<template>
  <div class="screen-full">
    <el-tooltip v-if="isFullscreen" content="退出全屏" placement="bottom">
      <svg-icon icon-class="head-exit-screen" @click="click" />
    </el-tooltip>
    <el-tooltip v-else content="全屏" placement="bottom">
      <svg-icon icon-class="head-full-screen" @click="click" />
    </el-tooltip>
  </div>
</template>

<script>
import screenfull from 'screenfull'

export default {
  name: 'Screenfull',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    click() {
      if (!screenfull.isEnabled) {
        this.$message({ message: '你的浏览器不支持全屏', type: 'warning' })
        return false
      }
      screenfull.toggle()
    },
    change() {
      this.isFullscreen = screenfull.isFullscreen
    },
    init() {
      if (screenfull.isEnabled) {
        screenfull.on('change', this.change)
      }
    },
    destroy() {
      if (screenfull.isEnabled) {
        screenfull.off('change', this.change)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.screen-full {
  padding: 0 10px;
}
</style>
